
package me.zhengjie.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.domain.AppWorkConfig;
import me.zhengjie.service.AppWorkConfigService;
import me.zhengjie.service.dto.AppWorkConfigQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author IQW
* @date 2022-07-13
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "任务配置管理")
@RequestMapping("/api/workConfig")
public class AppWorkConfigController {

    private final AppWorkConfigService appWorkConfigService;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('appWorkConfig:list')")
    public void exportAppWorkConfig(HttpServletResponse response, AppWorkConfigQueryCriteria criteria) throws IOException {
        appWorkConfigService.download(appWorkConfigService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询任务配置")
    @ApiOperation("查询任务配置")
    @PreAuthorize("@el.check('appWorkConfig:list')")
    public ResponseEntity<Object> queryAppWorkConfig(AppWorkConfigQueryCriteria criteria, Pageable pageable){

        return new ResponseEntity<>(appWorkConfigService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增任务配置")
    @ApiOperation("新增任务配置")
    @PreAuthorize("@el.check('appWorkConfig:add')")
    public ResponseEntity<Object> createAppWorkConfig(@Validated @RequestBody AppWorkConfig resources){
        return new ResponseEntity<>(appWorkConfigService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改任务配置")
    @ApiOperation("修改任务配置")
    @PreAuthorize("@el.check('appWorkConfig:edit')")
    public ResponseEntity<Object> updateAppWorkConfig(@Validated @RequestBody AppWorkConfig resources){
        appWorkConfigService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除任务配置")
    @ApiOperation("删除任务配置")
    @PreAuthorize("@el.check('appWorkConfig:del')")
    public ResponseEntity<Object> deleteAppWorkConfig(@RequestBody Integer[] ids) {
        appWorkConfigService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}